#include "mylabel.h"
#include <QMouseEvent>
#include <QDebug>

//创建一个 mylabel 对象，它首先调用父类 QLabel 的构造函数进行初始化，然后执行自己的初始化
mylabel::mylabel(QWidget *parent): QLabel(parent)
{
    //设置追踪鼠标
   // this->setMouseTracking(true);
}
//鼠标点击事件
void mylabel::mousePressEvent(QMouseEvent *ev)
{
    int i = ev->x();
    int j = ev->y();
    //sprinf
    //QString str = QString("abc %1 ** %2").arg(123).arg("mike");
    //str = abc 123 ** mike

    if(ev->button() == Qt::LeftButton)
    {
        qDebug() << "left";
    }
    else if(ev->button() == Qt::RightButton)
    {
        qDebug() << "right";
    }
    else if(ev->button() == Qt::MiddleButton)
    {
        qDebug() << "mid";
    }
    QString text = QString("<center><h1>Mouse Press: (%1,%2)</h1></center>").arg(i).arg(j);
    this->setText(text);

}

//鼠标移动事件
void mylabel::mouseMoveEvent(QMouseEvent *ev)
{
    QString text = QString("<center><h1>Mouse Move: (%1,%2)</h1></center>").arg(ev->x()).arg(ev->y());
    //this->setText(text);
}

//鼠标释放事件
void mylabel::mouseReleaseEvent(QMouseEvent *ev)
{
    QString text = QString("<center><h1>Mouse Release: (%1,%2)</h1></center>").arg(ev->x()).arg(ev->y());
    this->setText(text);
}

//进入窗口区域
void mylabel::enterEvent(QEnterEvent *event)
{
    QString text = QString("<center><h1>Mouse enter</h1></center>");
    this->setText(text);
}
//离开窗口区域
void mylabel::leaveEvent(QEvent *event)
{
    QString text = QString("<center><h1>Mouse Leave</h1></center>");
    this->setText(text);

}








